<?php
use backend\models\Permission;

/* @var $permissions */
/* @var $nodeList Permission[] */
/* @var $this \yii\web\View */
?>
<ul class="sidebar-menu">
    <?php foreach ($nodeList as $permission_model) {
        /** @var Permission $permission_model */
        if (in_array($permission_model->id, $permissions)) {
            if (!empty($permission_model->children)) {
                echo '<li class="treeview">';
                echo '<a href="#"><i class="fa fa-'.$permission_model->font.'"></i><span>' . $permission_model->description . '</span><i class="fa fa-angle-left pull-right"></i></a>';
                echo '<ul class="treeview-menu">';
                foreach ($permission_model->children as $child) {
                    if (in_array($child->id, $permissions)) {
                        echo '<li><a href="' . \yii\helpers\Url::toRoute($child->link) . '"><i class="fa fa-'.$child->font.'"></i>' . $child->description . '</a></li>';
                    }
                }
                echo '</ul>';
            } else {
                echo '<li><a href="/' . \yii\helpers\Url::toRoute($permission_model->link)  . '"><i class="fa fa-'.$permission_model->font.'"></i><span>' . $permission_model->description . '</span></a></li>';
            }
        }
    }
    ?>
</ul>
<?php
/** @var Permission $permission */
if (!empty(Yii::$app->controller->permission)) {

    $permission = Yii::$app->controller->permission;
    if ($permission->level == 2) {
        $link = $permission->link;
    } else{
        $link = $permission->parent->link;
    }
    $JS = <<<JS
var service = '$link';
$('.sidebar-menu a').each(function(){
    if ($(this).attr('href').substring(1) == service) {
        $(this).parents('li').addClass('active');
    }
})
JS;
    $this->registerJs($JS);
}
?>
